package com.ruoyi.activity.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.activity.domian.RedPacketConfig;
import com.ruoyi.activity.domian.RedPacketLimitConfig;
import com.ruoyi.activity.domian.RedPacketLimitParameter;
import com.ruoyi.activity.mapper.RedPacketConfigDao;
import com.ruoyi.activity.mapper.RedPacketLimitConfigDao;
import com.ruoyi.activity.mapper.RedPacketLimitParameterDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@DataSource(DataSourceType.SLAVE)
public class RedPacketLimitParameterService {
    @Resource
    private RedPacketLimitParameterDao redPacketLimitParameterDao;

    @Resource
    private RedPacketLimitConfigDao redPacketLimitConfigDao;

    @Resource
    private RedPacketConfigDao redPacketConfigDao;

    /**
     * 获取红包条件池列表
     * @author chengjian
     * @date 2021/9/9 6:18 下午
     * @param type 红包类型
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @param pageNum 第几页
     * @param pageSize 页码量
     */
    public Page<RedPacketLimitParameter> getList(Integer type, String beginTime, String endTime, int pageNum, int pageSize){
        LambdaQueryWrapper<RedPacketLimitParameter> queryWrapper = new LambdaQueryWrapper<>();
        if (type != null){
            queryWrapper.eq(RedPacketLimitParameter::getType, type);
        }

        if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            queryWrapper.ge(RedPacketLimitParameter::getCreateTime,beginTime);
            queryWrapper.le(RedPacketLimitParameter::getCreateTime,endTime);
        }
        return redPacketLimitParameterDao.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    /**
     * 获取具体一个条件信息
     * @author chengjian
     * @date 2021/9/9 6:19 下午
     * @param id 主键
     */
    public RedPacketLimitParameter getSingle(Long id){
        return redPacketLimitParameterDao.selectById(id);
    }

    /**
     * 更新红包条件信息
     * @author chengjian
     * @date 2021/9/9 6:19 下午
     * @param parameter 信息
     */
    public void update(RedPacketLimitParameter parameter){
        parameter.setUpdateTime(null);
        redPacketLimitParameterDao.updateById(parameter);
    }

    /**
     * 添加一个红包条件
     * @author chengjian
     * @date 2021/9/9 6:19 下午
     * @param parameter 信息
     */
    public void add(RedPacketLimitParameter parameter){
        redPacketLimitParameterDao.insert(parameter);
    }

    /**
     * 删除红包信息
     * @author chengjian
     * @date 2021/9/9 6:20 下午
     * @param ids id集合
     */
    public void del(List<Long> ids){
        redPacketLimitParameterDao.deleteBatchIds(ids);
    }


    /**
     * 获取一个红包配置下面所有条件id集合
     * @author chengjian
     * @date 2021/9/9 6:20 下午
     * @param id 红包配置id
     */
    public List<Long> getIdList(Long id){
        RedPacketConfig config = redPacketConfigDao.selectById(id);
        if (config == null){
            return new ArrayList<>();
        }

        RedPacketLimitConfig limitConfig = redPacketLimitConfigDao
                .selectOne(new LambdaQueryWrapper<RedPacketLimitConfig>().eq(RedPacketLimitConfig::getType,config.getType()));
        if (limitConfig == null){
            return new ArrayList<>();
        }

        return JSONArray.parseArray(limitConfig.getExtraParameter(),Long.class);
    }

    /**
     * 获取条件池树结构
     * @author chengjian
     * @date 2021/9/9 6:21 下午
     */
    public List<TreeSelect> getAll(){
        List<RedPacketLimitParameter> list = redPacketLimitParameterDao.selectList(new QueryWrapper<>());
        TreeSelect timeSelect = new TreeSelect();
        timeSelect.setId(-1L);
        timeSelect.setChildren(new ArrayList<>());
        timeSelect.setLabel("倒计时长");

        TreeSelect limitSelect = new TreeSelect();
        limitSelect.setId(-2L);
        limitSelect.setChildren(new ArrayList<>());
        limitSelect.setLabel("定向条件");


        list.forEach(e->{
            if (-timeSelect.getId() == e.getType()){
                timeSelect.getChildren().add(new TreeSelect(e.getId(),e.getParameter() + e.getDescription()));
            }

            if (-limitSelect.getId() == e.getType()){
                limitSelect.getChildren().add(new TreeSelect(e.getId(),e.getDescription()));
            }
        });


        List<TreeSelect> result = new ArrayList<>();
        result.add(limitSelect);
        result.add(timeSelect);
        return result;
    }
}
